package com.hinotoyk.testsystem.controller;

import com.hinotoyk.testsystem.entity.*;
import com.hinotoyk.testsystem.service.QuestionService;
import com.hinotoyk.testsystem.uitls.QuestionType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class QuestionController {
    @Resource
    private QuestionService serivice;

    @RequestMapping("/teacher/question/showpage")
    public String ShowPage(Model model, HttpServletRequest request){
        Teacher t = (Teacher)request.getSession().getAttribute("teacher");
        int teacherId = 1;
        if(t != null)
            teacherId = t.getTeacherId();

        List<SGQuestion> sgList = serivice.FindSGQuestionByTeacherId(teacherId);
        List<PLQuestion> plList = serivice.FindPLQuestionByTeacherId(teacherId);
        List<TOFQuestion> tofList = serivice.FindTOFQuestionByTeacherId(teacherId);
        List<EssayQuestion> essayList = serivice.FindEssayQuestionByTeacherId(teacherId);
        List<FillingQuestion> fillList = serivice.FindFillingQuestionByTeacherId(teacherId);

        if(sgList != null)
            model.addAttribute("sgList", sgList);
        if(plList != null)
            model.addAttribute("plList", plList);
        if(tofList != null)
            model.addAttribute("tofList", tofList);
        if(essayList != null)
            model.addAttribute("essayList", essayList);
        if(fillList != null)
            model.addAttribute( "fillList", fillList);

        return "teacher/question/showpage";
    }

    @RequestMapping("/teacher/question/addpage")
    public String AddPage(Model model){
        return "teacher/question/addpage";
    }

    // 该函数用于表单中提交已经填写好了的题目，进行提交使用,如
    // <form action="/teacher/question/add/1"> </form>
    @RequestMapping("/teacher/question/add/{type}")
    public String AddQuestion(@PathVariable(value = "type") int type, Model model, HttpServletRequest request) {
        boolean flag = false;
        Teacher t = (Teacher)request.getSession().getAttribute("teacher");
        int teacherId = 1;
        if(t != null)
            teacherId = t.getTeacherId();
        // 根据type选择serivice的add方法
        if (type == QuestionType.SGQuestion.getType()) {
            SGQuestion q = new SGQuestion();
            q.setSgQuestion(request.getParameter("question"));
            q.setChoiceA(request.getParameter("A"));
            q.setChoiceB(request.getParameter("B"));
            q.setChoiceC(request.getParameter("C"));
            q.setChoiceD(request.getParameter("D"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            q.setTeacherId(teacherId);
            flag = serivice.AddSGQuestion(q);
        } else if (type == QuestionType.FillingQuestion.getType()) {
            FillingQuestion q = new FillingQuestion();
            q.setFillQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            q.setTeacherId(teacherId);
            flag = serivice.AddFillingQuestion(q);
        } else if (type == QuestionType.EssayQuestion.getType()) {
            EssayQuestion q = new EssayQuestion();
            q.setEssayQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            q.setTeacherId(teacherId);
            flag = serivice.AddEssayQuestion(q);
        } else if (type == QuestionType.PLQuestion.getType()) {
            PLQuestion q = new PLQuestion();
            q.setPlQuestion(request.getParameter("question"));
            q.setChoiceA(request.getParameter("A"));
            q.setChoiceB(request.getParameter("B"));
            q.setChoiceC(request.getParameter("C"));
            q.setChoiceD(request.getParameter("D"));

            // 多项选择题中,复选框是一个字符串数组
            String [] answerStrs = request.getParameterValues("answer");
            String answer = new String();
            for(int i = 0; i < answerStrs.length; ++i)
                answer += answerStrs[i];
            q.setAnswer(answer);

            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            q.setTeacherId(teacherId);
            q.toString();
            flag = serivice.AddPLQuestion(q);
        } else if (type == QuestionType.TOFQuestion.getType()) {
            TOFQuestion q = new TOFQuestion();
            q.setTofQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            q.setTeacherId(teacherId);
            flag = serivice.AddTOFQuestion(q);
        }

        //返回的status页面，展示操作的结果
        model.addAttribute("title", "增添问题");
        if (flag) {
            model.addAttribute("status", "成功");
        } else {
            model.addAttribute("status", "失败");
        }
        return "teacher/question/statuspage";
    }

    // 该方法用于问题展示页面中，题目下方的删除按钮或删除链接根据题目id进行删除使用，如
    // <a herf = "/teacher/question/delete/1/0001"> 删除该题目 </a>
    @RequestMapping("/teacher/question/delete/{type}/{id}")
    public String DeleteQuestion(@PathVariable(value = "type") int type, @PathVariable( value = "id") int id, Model model){
        boolean flag = false;
        if(type == QuestionType.SGQuestion.getType()){
            flag = serivice.DeleteSGQuestion(id);
        }else if(type == QuestionType.FillingQuestion.getType()){
            flag = serivice.DeleteFillingQuestion(id);
        }else if(type == QuestionType.EssayQuestion.getType()){
            flag = serivice.DeleteEssayQuestion(id);
        }else if(type == QuestionType.PLQuestion.getType()){
            flag = serivice.DeletePLQuestion(id);
        }else if(type == QuestionType.TOFQuestion.getType()){
            flag = serivice.DeleteTOFQuestion(id);
        }

        //返回的status页面，展示操作的结果
        model.addAttribute("title", "删除问题");
        if (flag) {
            model.addAttribute("status", "成功");
        } else {
            model.addAttribute("status", "失败");
        }

        return "teacher/question/stutaspage";
    }

    // 该方法对应修改问题的页面
    @RequestMapping("/teacher/question/updatepage/{type}/{id}")
    public String UpdatePage(@PathVariable(value = "type") int type, @PathVariable(value = "id") int id, Model model){
        if(type == QuestionType.SGQuestion.getType()){
            SGQuestion question = serivice.FindSGQuestion(id);
            model.addAttribute("question", question);
        }else if(type == QuestionType.FillingQuestion.getType()){
            FillingQuestion question = serivice.FindFillingQuestion(id);
            model.addAttribute("question", question);
        }else if(type == QuestionType.EssayQuestion.getType()){
            EssayQuestion question = serivice.FindEssayQuestion(id);
            model.addAttribute("question", question);
        }else if(type == QuestionType.PLQuestion.getType()){
            PLQuestion question = serivice.FindPLQuestion(id);
            model.addAttribute("question", question);

            if(question != null) {
                for (int i = 0; i < question.getAnswer().length(); ++i) {
                    model.addAttribute(String.valueOf(question.getAnswer().charAt(i)), true);
                }
            }
        }else if(type == QuestionType.TOFQuestion.getType()){
            TOFQuestion question = serivice.FindTOFQuestion(id);
            model.addAttribute("question", question);
        }

        if(model.getAttribute("question") != null){
            model.addAttribute("type", type);
            model.addAttribute("id", id);
        }else{
            model.addAttribute("type", QuestionType.ErrorQuestion.getType());
        }

        return "teacher/question/updatepage";
    }

    // 该方法用于修改问题后，准备将问题进行更新的链接，如
    // <form action="/teacher/question/update/1"> </form>
    @RequestMapping("/teacher/question/update/{type}")
    public String UpdateQuestion(@PathVariable(value = "type") int type, Model model, HttpServletRequest request){
        Teacher t = (Teacher)request.getSession().getAttribute("teacher");
        int teacherId = 1;
        if(t != null)
            teacherId = t.getTeacherId();
        boolean flag = false;
        if (type == QuestionType.SGQuestion.getType()) {
            SGQuestion q = new SGQuestion();
            q.setSgQuestionId(Integer.valueOf(request.getParameter("id")));
            q.setSgQuestion(request.getParameter("question"));
            q.setChoiceA(request.getParameter("A"));
            q.setChoiceB(request.getParameter("B"));
            q.setChoiceC(request.getParameter("C"));
            q.setChoiceD(request.getParameter("D"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            // 这里需要的是修改题目的老师的teacherId
            q.setTeacherId(teacherId);
            flag = serivice.UpdateSGQuestion(q);
        } else if (type == QuestionType.FillingQuestion.getType()) {
            FillingQuestion q = new FillingQuestion();
            q.setFillQuestionId(Integer.valueOf(request.getParameter("id")));
            q.setFillQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            // 这里需要的是修改题目的老师的teacherId
            q.setTeacherId(teacherId);
            flag = serivice.UpdateFillingQuestion(q);
        } else if (type == QuestionType.EssayQuestion.getType()) {
            EssayQuestion q = new EssayQuestion();
            q.setEssayQuestionId(Integer.valueOf(request.getParameter("id")));
            q.setEssayQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            // 这里需要的是修改题目的老师的teacherId
            q.setTeacherId(teacherId);
            flag = serivice.UpdateEssayQuestion(q);
        } else if (type == QuestionType.PLQuestion.getType()) {
            PLQuestion q = new PLQuestion();
            q.setPlQuestionId(Integer.valueOf(request.getParameter("id")));
            q.setPlQuestion(request.getParameter("question"));
            q.setChoiceA(request.getParameter("A"));
            q.setChoiceB(request.getParameter("B"));
            q.setChoiceC(request.getParameter("C"));
            q.setChoiceD(request.getParameter("D"));

            // 多项选择题中,复选框是一个字符串数组
            String [] answerStrs = request.getParameterValues("answer");
            String answer = new String();
            for(int i = 0; i < answerStrs.length; ++i)
                answer += answerStrs[i];
            q.setAnswer(answer);

            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            // 这里需要的是修改题目的老师的teacherId
            q.setTeacherId(teacherId);
            flag = serivice.UpdatePLQuestion(q);
        } else if (type == QuestionType.TOFQuestion.getType()) {
            TOFQuestion q = new TOFQuestion();
            q.setTofQuestionId(Integer.valueOf(request.getParameter("id")));
            q.setTofQuestion(request.getParameter("question"));
            q.setAnswer(request.getParameter("answer"));
            q.setScore(Integer.valueOf(request.getParameter("score")));
            q.setDifficult(Integer.valueOf(request.getParameter("difficult")));
            // 这里需要的是修改题目的老师的teacherId
            q.setTeacherId(teacherId);
            flag = serivice.UpdateTOFQuestion(q);
        }

        //返回的status页面，展示操作的结果
        model.addAttribute("title", "更改问题");
        if (flag) {
            model.addAttribute("status", "成功");
        } else {
            model.addAttribute("status", "失败");
        }

        return "teacher/question/statuspage";
    }
}
